import tempfile

from injector import inject
from dataclasses import dataclass

from src.schema.chat_schema import UploadSchemaFile
from src.service import ChatService
from src.schema import ChatSchema,UploadImageSchema,UploadAudioSchema,UploadTextAudioSchema
from flask import request,send_file
from src.pkg import validate_error_json, success_json
import  requests as re

@inject
@dataclass
class AppHandler:
    chat_service: ChatService
    def chat(self):
        req = ChatSchema()
        print("zzzzzzzzzzzzzzzzzzzzzz")
        if not req.validate():
            return validate_error_json(req.errors)
        data = self.chat_service.chat(req.query.data)
        return success_json(data)
    def upload_file(self):
        req = UploadImageSchema()
        if not req.validate():
            return validate_error_json(req.errors)
        data = self.chat_service.upload_image(req.url.data,req.query.data)
        return success_json(data)
    def upload_img(self):
        req = UploadSchemaFile()
        if not req.validate():
            return validate_error_json(req.errors)
        print("======================")
        print(req.file.data)
        data = self.chat_service.upload(req.file.data)
        print(data)
        return success_json(data)


    def audio_text(self):
        req = UploadAudioSchema()
        if not req.validate():
            return validate_error_json(req.errors)
        data = self.chat_service.audio_text(req.file.data)
        return success_json(data)
    def text_audio(self):
        req = UploadTextAudioSchema(request.args)
        if not req.validate():
            return validate_error_json(req.errors)
        data = self.chat_service.text_audio(req.query.data)
        print(data)
        return data
    def sim(self):
        req = ChatSchema(request.args)
        if not req.validate():
            return validate_error_json(req.errors)
        data = self.chat_service.sim(req.query.data)
        return success_json(data)



